<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Admin\Stats\Settlement\All as StatsAll;
use App\Models\Admin\Stats\Settlement\Game as StatsGame;
use App\Models\Admin\Stats\Settlement\GameScene as StatsGameScene;
use App\Models\Admin\StatsTask\Settlement\All as TaskAll;
use App\Models\Admin\StatsTask\Settlement\Game as TaskGame;
use App\Models\Admin\StatsTask\Settlement\GameScene as TaskGameScene;
use App\Core\Traits\Numeral;

class CreateStatsSettlementTable extends Migration
{
    use Numeral;

    /**
     * 统计任务表字段
     */
    private function makeStatsTask(Blueprint $table)
    {
        $table->id();
        $table->unsignedInteger('timed');
        $table->string('plat_code', 64)->comment('平台code');

        $table->tinyInteger('attempts')->comment('任务尝试次数')->default(0);
        $table->tinyInteger('status')->comment('状态：1初始化，2推送到队列，3开始处理，4成功，5失败')->default(1)->index();

        $table->unsignedInteger('created');
        $table->unsignedInteger('updated');
    }

    /**
     * 统计数据表字段
     */
    private function makeStats(Blueprint $table)
    {
        $table->id();
        $table->unsignedInteger('timed');
        $table->string('plat_code', 64)->comment('平台code')->index();
        $table->string('currency', 16)->comment('币种code')->index();

        $table->integer('doc_count')->comment('记录条数')->default(0);
        $table->integer('player_count')->comment('游戏人数')->default(0);
        $table->decimal('bet_base', $this->decimals(), $this->precision())->comment('底分')->default(0);
        $table->decimal('bet_num', $this->decimals(), $this->precision())->comment('玩家投注')->default(0);
        $table->decimal('gain_player', $this->decimals(), $this->precision())->comment('玩家盈亏')->default(0);
        $table->decimal('amount', $this->decimals(), $this->precision())->comment('平台收益')->default(0);
        $table->decimal('rtp', $this->decimals(), $this->precision())->comment('RTP')->default(1);

        $table->json('usd')->comment('换算USD');

        $table->unsignedInteger('created');
        $table->unsignedInteger('updated');
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 统计任务表【All】
         */
        $tableName = TaskAll::tableName();
        // 创建表结构
        TaskAll::schemaConnection()->create($tableName, function (Blueprint $table) use ($tableName) {
            $this->makeStatsTask($table);
            // 唯一索引
            $table->unique(['timed', 'plat_code'], "{$tableName}_unique");
        });
        // 表名注释
        TaskAll::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--结算--all'");
        /**
         * 统计数据表【All】
         */
        $tableName = StatsAll::tableName();
        // 创建表结构
        StatsAll::schemaConnection()->create($tableName, function (Blueprint $table) use ($tableName) {
            $this->makeStats($table);
            // 唯一索引
            $table->unique(['timed', 'plat_code', 'currency'], "{$tableName}_unique");
        });
        // 表名注释
        StatsAll::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--结算--all'");

        /**
         * 统计任务表【Game】
         */
        $tableName = TaskGame::tableName();
        // 创建表结构
        TaskGame::schemaConnection()->create($tableName, function (Blueprint $table) use ($tableName) {
            $this->makeStatsTask($table);
            $table->string('game_id', 32)->comment('游戏code');
            // 唯一索引
            $table->unique(['timed', 'plat_code', 'game_id'], "{$tableName}_unique");
        });
        // 表名注释
        TaskGame::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--结算--game'");
        /**
         * 统计数据表【Game】
         */
        $tableName = StatsGame::tableName();
        // 创建表结构
        StatsGame::schemaConnection()->create($tableName, function (Blueprint $table) use ($tableName) {
            $this->makeStats($table);
            $table->string('game_id', 32)->comment('游戏code')->index();
            // 唯一索引
            $table->unique(['timed', 'plat_code', 'game_id', 'currency'], "{$tableName}_unique");
        });
        // 表名注释
        StatsGame::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--结算--game'");

        /**
         * 统计任务表【GameScene】
         */
        $tableName = TaskGameScene::tableName();
        // 创建表结构
        TaskGameScene::schemaConnection()->create($tableName, function (Blueprint $table) use ($tableName) {
            $this->makeStatsTask($table);
            $table->string('game_id', 32)->comment('游戏code');
            $table->integer('scene_id')->comment('场景ID');
            // 唯一索引
            $table->unique(['timed', 'plat_code', 'game_id', 'scene_id'], "{$tableName}_unique");
        });
        // 表名注释
        TaskGameScene::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--结算--game--scene'");
        /**
         * 统计数据表【GameScene】
         */
        $tableName = StatsGameScene::tableName();
        // 创建表结构
        StatsGameScene::schemaConnection()->create($tableName, function (Blueprint $table) use ($tableName) {
            $this->makeStats($table);
            $table->string('game_id', 32)->comment('游戏code')->index();
            $table->integer('scene_id')->comment('场景ID')->index();
            // 唯一索引
            $table->unique(['timed', 'plat_code', 'game_id', 'scene_id', 'currency'], "{$tableName}_unique");
        });
        // 表名注释
        StatsGameScene::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--结算--game--scene'");

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // 统计任务表【All】
        TaskAll::schemaConnection()->dropIfExists(TaskAll::tableName());
        // 统计数据表【All】
        StatsAll::schemaConnection()->dropIfExists(StatsAll::tableName());

        // 统计任务表【Game】
        TaskGame::schemaConnection()->dropIfExists(TaskGame::tableName());
        // 统计数据表【Game】
        StatsGame::schemaConnection()->dropIfExists(StatsGame::tableName());

        // 统计任务表【GameScene】
        TaskGameScene::schemaConnection()->dropIfExists(TaskGameScene::tableName());
        // 统计数据表【GameScene】
        StatsGameScene::schemaConnection()->dropIfExists(StatsGameScene::tableName());
    }
}
